﻿using Coldairarrow.Entity.Backstage.UserData;
using Coldairarrow.Entity.Frontstage.Valickets;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Frontstage.Valickets
{
    public class BS_UserPUseLogBusiness : BaseBusiness<BS_UserPUseLog>, IBS_UserPUseLogBusiness, ITransientDependency
    {
        readonly Operator _operator;
        public BS_UserPUseLogBusiness(IDbAccessor db,Operator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口

        public async Task<PageResult<BS_UserPUseLog>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<BS_UserPUseLog>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<BS_UserPUseLog, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<PageResult<GetUPLogDTO>> GetPDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable().Where(x => x.UserId.Equals(_operator.UserId)).Select(x =>new GetUPLogDTO { 
                Id =x.Id,
                PointNum = x.PointNum,
                Purpose = x.Purpose,
                Type = x.Type,
                Time = x.Time
            });

            var where = LinqHelper.True<GetUPLogDTO>();
            var search = input.Search;

            return await q.Where(where).GetPageResultAsync(input);
        }

        /// <summary>
        /// 获取用户积分统计
        /// </summary>
        /// <returns></returns>
        public PointSum GetUSumData()
        {
            PointSum data = new PointSum();
            data.SumPointNum = GetIQueryable().Where(x => x.UserId.Equals(_operator.UserId) && x.Type == 0).Sum(x => (int)x.PointNum);
            data.UsePointNum = -1 * GetIQueryable().Where(x => x.UserId.Equals(_operator.UserId) && x.Type == -1).Sum(x => (int)x.PointNum);
            data.NUPointNum = data.SumPointNum - data.UsePointNum;

            return data;
        }

        /// <summary>
        /// 用户获取积分
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task UserGetPoint(GetPointDto input)
        {
            BS_UserPUseLog model = new BS_UserPUseLog();
            model.Id = IdHelper.GetId();
            model.CreateTime = DateTime.Now;
            model.Deleted = false;
            model.CreatorId = _operator.UserId;
            model.UserId = _operator.UserId;
            model.Type = 0;
            model.PointNum = input.Point;
            model.Purpose = input.Note;
            model.Time = DateTime.Now;

            var umList = Db.GetIQueryable<BS_UserICPoint>().Where(x => x.UserId.Equals(_operator.UserId)).ToList();
            if (umList.Count > 0)
            {
                umList[0].Point += input.Point;
                await Db.UpdateAsync(umList[0]);
                await InsertAsync(model);
            }
            else
            {
                BS_UserICPoint usModel = new BS_UserICPoint();
                usModel.Id = IdHelper.GetId();
                usModel.CreateTime = DateTime.Now;
                usModel.Deleted = false;
                usModel.CreatorId = _operator.UserId;
                usModel.UserId = _operator.UserId;
                usModel.Income = 0;
                usModel.Point = input.Point;

                await Db.InsertAsync(usModel);
                await InsertAsync(model);
            }


        }

        public async Task<BS_UserPUseLog> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(BS_UserPUseLog data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(BS_UserPUseLog data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion




        #region 私有成员

        #endregion
    }
}